//
//  MVVM_VM.m
//  DesignPattern
//
//  Created by jun on 2021/4/27.
//

#import "MVVM_VM.h"
#import "MVVM-M.h"
#import <ReactiveObjC/RACSignal.h>
#import <ReactiveObjC/RACSubscriber.h>

@interface MVVM_VM ()

@property (nonatomic,strong) RACCommand *updateDataCommand;
@property MVVM_M *model;

@end

@implementation MVVM_VM

-(void)getUserData:(ClientCompelete)compelete{
    __weak typeof(self) wSelf = self;
    [RequestManager getUserData:^(BOOL success, NSDictionary * _Nonnull dic, NSString * _Nonnull msg, NSError * _Nonnull error) {
        __strong typeof(wSelf) sSelf = wSelf;
        if (success) {
            if ([dic isKindOfClass:[NSDictionary class]]&&[dic[@"data"] isKindOfClass:[NSDictionary class]]) {
                MVVM_M *model = [MVVM_M new];
                model.title =dic[@"data"][@"title"];
                model.desc =dic[@"data"][@"desc"];
                sSelf.model = model;
            }
            compelete(success,dic,msg,error);
        }
    }];
}

- (RACCommand *)updateDataCommand {
    if (!_updateDataCommand) {
//        @weakify(self);
        NSLog(@"alloc");
        _updateDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
//            @strongify(self);
            ///type 1.排序 2.筛选
//            NSInteger type = 1;//[input[@"type"] integerValue];
            //RAC 3
            NSLog(@"initWithSignalBlock");
            NSLog(@"%@",input);
            RACSignal * signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [subscriber sendNext:input];
                [subscriber sendCompleted];
                return nil;
            }];
            
            return signal;
        }];
//        忽略第多少步 从一开始 //负数全部忽略，0不忽略
        [[_updateDataCommand.executing skip:0] subscribeNext:^(NSNumber * _Nullable x) {
            if ([x boolValue]) {
                NSLog(@"开始执行");
            }else{
                NSLog(@"执行结束");
            }
        }];
    }
    return _updateDataCommand;
}

@end
